package com.leng.controller;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leng.VO.UserVO;
import com.leng.entity.User;
import com.leng.exception.BusinessException;
import com.leng.response.Result;
import com.leng.response.ResultCode;
import com.leng.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author leng
 * @since 2022-08-21
 */
@Api(tags = "系统用户接口", value = "系统用户模块")
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/deleteUsers")
    @ApiOperation(value = "删除单个用户", notes = "删除单个用户")
    public Result deleteUsers(Integer id) {
        boolean b = userService.removeBatchByIds(Collections.singleton(id));
        return Result.ok().data("b", b);
    }

    @GetMapping("/getUsers")
    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    public Result getUsers() {
        List<User> userList = userService.list(null);
        return Result.ok().data("userList", userList);
    }

    @GetMapping("/getUsersById")
    @ApiOperation(value = "查询单个用户", notes = "查询单个用户")
    public Result getUsersById() {
        User byId = userService.getById(500);
        if (byId == null) {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return Result.ok().data("userList", byId);
    }

    @PostMapping("/getUserByPage")
    @ApiOperation(value = "分页+条件+连接查询用户信息", notes = "分页+条件+连接")
    public Result getUserByPage(@RequestParam(value = "current", required = true, defaultValue = "1") Integer current,
                                @RequestParam(value = "size", required = true, defaultValue = "7") Integer size,
                                @RequestBody UserVO userVO) {
        Page<User> page = new Page<>(current, size);
        Page<User> userByPage = userService.getUserByPage(page, userVO);
        return Result.ok().data("userByPage", userByPage);
    }

    @ApiOperation(value = "导出用户信息")
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,
                            @RequestBody(required = false) UserVO userVO) throws IOException {
        //根据条件查询
        userService.exportExcel(response, userService.getUserListToExcel(userVO));
    }

    //导入
    @PostMapping("/import")
    public void imp(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        // 通过 javabean的方式读取Excel内的对象，但是要求表头必须是英文，跟javabean的属性要对应起来
        List<User> list = reader.readAll(User.class);
        userService.saveBatch(list);
    }

    //用户登录
    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public Result login(@RequestParam(value = "username", required = true) String username,
                        @RequestParam(value = "password", required = true) String password,
                        HttpServletRequest request) {
        //判断是否为空
        if (StringUtils.isEmpty(username)) {
            throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.PARAM_IS_BLANK.getMessage());
        }
        String token = userService.login(username, password);
        return Result.ok().data("token", token);
    }

    @ApiOperation(value = "用户添加", notes = "用户添加")
    @GetMapping("/addUser")
    //用户添加
    public Result addUser(User user) {
        User userByUsername = userService.getUserByUsername(user.getUsername());
        //判断当前数据库是否存在该用户名
        if (userByUsername != null) {
            throw new BusinessException(ResultCode.USER_ACCOUNT_ALREADY_EXIST.getCode(),
                    ResultCode.PARAM_IS_BLANK.getMessage());
        }
        boolean save = userService.save(user);
        return Result.ok().success(save);
    }
}